package com.example.demo.controller;

import com.example.demo.factory.ShapeFactory;
import com.example.demo.observer.CurrentConditionsDisplay;
import com.example.demo.observer.WeatherData;
import com.example.demo.proxy.ProxySubject;
import com.example.demo.singleton.Singleton;
import com.example.demo.strategy.Context;
import com.example.demo.strategy.OperationAdd;
import com.example.demo.strategy.OperationSubtract;
import com.example.demo.strategy.Strategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private Singleton singleton;

    @Autowired
    private ShapeFactory shapeFactory;

    @Autowired
    private ProxySubject proxySubject;

    @Autowired
    private WeatherData weatherData;

    @Autowired
    private CurrentConditionsDisplay currentConditionsDisplay;

    @Autowired
    private Context context;

    @GetMapping("/singleton")
    public String testSingleton() {
        singleton.doSomething();
        return "Singleton test done";
    }

    @GetMapping("/factory")
    public String testFactory() {
        shapeFactory.getShape("CIRCLE").draw();
        return "Factory test done";
    }

    @GetMapping("/proxy")
    public String testProxy() {
        proxySubject.request();
        return "Proxy test done";
    }

    @GetMapping("/observer")
    public String testObserver() {
        weatherData.setMeasurements(25, 60, 1013);
        return "Observer test done";
    }

    @GetMapping("/strategy")
    public String testStrategy() {
        Strategy addStrategy = new OperationAdd();
        Strategy subtractStrategy = new OperationSubtract();
        double resultAdd = context.executeStrategy(addStrategy, 5, 3);
        double resultSubtract = context.executeStrategy(subtractStrategy, 5, 3);
        System.out.println("加法结果: " + resultAdd);
        System.out.println("减法结果: " + resultSubtract);
        return "Strategy test done";
    }
}    